package com.baizhi.controller;


import com.baizhi.dto.request.TeacherDTO;
import com.baizhi.dto.response.StudentDTO;
import com.baizhi.entity.Course;
import com.baizhi.entity.Teacher;
import com.baizhi.entity.User;
import com.baizhi.exception.CodeEnum;
import com.baizhi.service.CourseService;
import com.baizhi.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name="教师管理模块", description = "教师管理模块的控制器")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;


    /**
     * 教师注销
     * @param userId
     * @return
     */
    @GetMapping("/teacher/logOut")
    public ResponseEntity<String> logOutTeacher(@RequestParam("userId") Integer userId){

        Boolean flag = teacherService.logOutTeacher(userId);
        if(flag){
            return new ResponseEntity<>("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }


    @PostMapping("/teacher/update")
    public ResponseEntity<CodeEnum> updateTeacher(@RequestBody @Valid User user){
        boolean flag = teacherService.updateTeacher(user);
        if(flag){
            return new ResponseEntity<>(CodeEnum.success,HttpStatusCode.valueOf(200));
        }else{
            return  new ResponseEntity<>(CodeEnum.user_update_fail,HttpStatusCode.valueOf(407));
        }
    }


    @GetMapping("/teacher/updateScore")
    public ResponseEntity<String> updateScore(@RequestParam("stuId") Integer stuId,
                                              @RequestParam("courseId") Integer courseId,
                                              @RequestParam("score") Integer score){
        Integer row = teacherService.updateScoreByStuIdAndCourseId(stuId, courseId, score);
        if(row == 0 ){
            return ResponseEntity.ofNullable("修改失败");
        }else{
            return ResponseEntity.ok("修改成功");
        }

    }


    @GetMapping("/teacher/deleteById")
    public ResponseEntity<String> deleteMyCourseByUserIdAndCourseId(@RequestParam("stuId") Integer stuId,
                                                                    @RequestParam("courseId") Integer courseId){

        Integer row = teacherService.deleteMyCourseByUserIdAndCourseId(stuId, courseId);
        if(row!=0){
            return new ResponseEntity<>("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }

    @GetMapping("/teacher/stus")
    public ResponseEntity<List<StudentDTO>> getStudentsByCourseId(@RequestParam("courseId") Integer courseId){
        List<StudentDTO> list = teacherService.getStudentsByCourseId(courseId);
        if(list == null || list.isEmpty()){
            return ResponseEntity.notFound().build();
        }else{
            return ResponseEntity.ok(list);
        }
    }

    @GetMapping("/teacher/courses")
    public ResponseEntity< Map<String,Object>> showCourseByTeacherId(@RequestParam("userId") Integer userId){
        //1. 根据userId 获取 教师ID pk_tid
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",userId).select("pk_tid","name");
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);

        //2. 根据教师ID查询课程
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("teacher_id",teacher.getPkTid());
        List<Course> list = courseService.list(courseQueryWrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("teacher",teacher.getName());

        if(map == null || map.isEmpty()){
            //没有元素返回
            return ResponseEntity.notFound().build(); //返回的就是一个404
        }else{
            //返回集合
            return ResponseEntity.ok(map);
        }

    }


    @PostMapping("/teacher/regist")
    public ResponseEntity<String> teacherRegist(@RequestBody @Valid TeacherDTO teacherDTO){
        System.out.println(teacherDTO);
        teacherService.teacherRegist(teacherDTO);
        return new ResponseEntity<>("注册成功", HttpStatusCode.valueOf(200));
    }
}
